1Writer リピートタスク一括コピー3
[]1Writer リピートタスク 一括コピー4
2024/10/31
N日おきが機能しない問題を修正
タスクの技術形式をN日おきからN日毎に変更(1日おき=2日毎)
⚠️週次などが機能しない
タスクの記述形式
@repeat 日次: 毎日
@repeat 週次: 毎週
@repeat 月次 <日>: 毎月特定の日(例: @repeat 月次 15)
@repeat 年次 <月>-<日>: 毎年特定の日付(例: @repeat 年次 12-25)
@repeat <n>日毎 <基準日>: 指定日数ごとに基準日からの間隔でリピート(例: @repeat 3日毎 2024-11-01)
@repeat <曜日><曜日>...: 特定の曜日にリピート(例: @repeat 月, 水, 金)
@repeat 平日: 月曜日から金曜日
code:js
function generateRepeatTasks() {
const filePath = editor.getFolderPath() + "/リピートタスク.md";
// 現在開いているデイリーノートの日付を取得
const dailyNoteDate = getDailyNoteDate();
if (!dailyNoteDate) {
ui.hudError("日付形式が無効です。デイリーノートのファイル名を確認してください。");
return;
}
// 現在開いているデイリーノートのカーソル位置を取得
const cursorPosition = editor.getSelectedRange()0;
// リピートタスクファイルを開き、成功後にgetTextで内容を取得
editor.openFile(filePath, 'edit', () => {
const content = editor.getText();
if (!content) {
ui.hudError("リピートタスク.mdが読み込めませんでした。ファイルが存在するか確認してください。");
return;
}
const tasks = content.split('\n');
const newTasks = [];
tasks.forEach(task => {
const repeatMatch = task.match(/@repeat (.+)/);
if (!repeatMatch) {
// @repeat指定がないタスクはそのままコピーし、末尾に⟳を追加
const taskText = task.trim();
if (taskText.startsWith("###")) {
// セクション見出しの場合はそのまま
newTasks.push(taskText);
} else {
// 通常のタスクには⟳を追加
newTasks.push(${taskText} ⟳);
}
return;
}
const repeatRule = repeatMatch1;
const isRepeatDay = checkRepeatDay(dailyNoteDate, repeatRule);
// デイリーノートの日付がリピート日に該当する場合のみコピー
if (isRepeatDay) {
const taskText = task.replace(/@repeat .+/, '').trim();
if (taskText.startsWith("###")) {
// セクション見出しには⟳を付けずにそのまま追加
newTasks.push(taskText);
} else {
// 通常のタスクには⟳を追加
newTasks.push(${taskText} ⟳);
}
}
});
if (newTasks.length > 0) {
const newFilePath = editor.getFolderPath() + /${dailyNoteDate}.md;
// カーソル位置を取得し、そこに新しいタスクを挿入
editor.openFile(newFilePath, 'edit', () => {
const tasksToInsert = newTasks.join('\n') + '\n';
editor.replaceTextInRange(cursorPosition, cursorPosition, tasksToInsert);
ui.hudSuccess("次回のリピートタスクを生成しました!");
});
} else {
ui.hudError("一致するタスクがありません。");
}
});
}
// 開いているデイリーノートの日付を取得(ファイル名が "YYYY-MM-DD.md" の形式であると仮定)
function getDailyNoteDate() {
const fileName = editor.getFileName();
const dateMatch = fileName.match(/^(\d{4}-\d{2}-\d{2})/);
return dateMatch ? dateMatch1 : null;
}
// n日毎のリピート日を判定する関数
function checkRepeatDay(noteDate, rule) {
const noteDateObj = new Date(noteDate);
const dayIntervalMatch = rule.match(/^(\d+)日毎(?: (\d{4}-\d{2}-\d{2}))?$/);
if (dayIntervalMatch) {
// n日毎の判定
const nDays = parseInt(dayIntervalMatch1, 10);
const baseDate = dayIntervalMatch2 ? new Date(dayIntervalMatch2) : noteDateObj;
// 基準日との差分がnの倍数かどうかをチェック
const diffDays = Math.floor((noteDateObj - baseDate) / (1000 * 60 * 60 * 24));
return diffDays >= 0 && diffDays % nDays === 0;
}
return false;
}
// 日付を "YYYY-MM-DD" フォーマットで文字列に変換
function formatDate(date) {
const yyyy = date.getFullYear();
const mm = String(date.getMonth() + 1).padStart(2, '0');
const dd = String(date.getDate()).padStart(2, '0');
return ${yyyy}-${mm}-${dd};
}
generateRepeatTasks();